﻿using HZY.Framework.DependencyInjection;
using Long.DAL;
using Long.Helper;
using Long.Model;
using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Long.BLL
{
    public class RecipeManager : IScopedSelfDependency
    {
        public readonly RecipeService _recipeService;

        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="recipeService"></param>
        public RecipeManager(RecipeService recipeService)
        {
            _recipeService = recipeService;
        }

        public async Task<BaseResult> AddRecipeAsyncManager(RecipeAddDto request)
        {
            //判断是否存在相同产品类型
            bool isExist = await _recipeService.ExistAsync(x => x.产品类型 == request.产品类型);
            if (isExist)
            {
                return new BaseResult() { ResultStatus = SystemEnums.ResultStatus.Fail, Msg = "存在相同产品类型" };
            }
            RecipeEntity entity = request.Adapt<RecipeEntity>();
            int res = await _recipeService.InsertAsync(entity);
            if (res > 0)
            {
                return new BaseResult() { ResultStatus = SystemEnums.ResultStatus.Success };
            }
            else
            {
                return new BaseResult() { ResultStatus = SystemEnums.ResultStatus.Fail, Msg = $"添加 {request.产品类型} 失败" };
            }
        }

        public async Task<BaseResult> UpdateRecipeAsyncManager(RecipeUpdateDto request)
        {
            bool isExist = await _recipeService.ExistAsync(c => c.产品类型 == request.产品类型 && c.Id != request.Id);
            if (isExist)
            {
                return new BaseResult() { ResultStatus = SystemEnums.ResultStatus.Fail, Msg = "修改的产品类型已存在" };
            }

            RecipeEntity entity = request.Adapt<RecipeEntity>();
            bool res = await _recipeService.UpdateAsync(entity);
            if (res)
            {
                return new BaseResult() { ResultStatus = SystemEnums.ResultStatus.Success};
            }
            return new BaseResult() { ResultStatus = SystemEnums.ResultStatus.Fail, Msg = $"更新 {request.产品类型} 失败" };
        }

        public async Task<BaseResult> DeleteRecipeAsyncManager(RecipeDeleteDto request)
        {
            RecipeEntity entity = request.Adapt<RecipeEntity>();
            bool res = await _recipeService.DeleteAsync(entity);
            if (res)
            {
                return new BaseResult() { ResultStatus = SystemEnums.ResultStatus.Success };
            }
            return new BaseResult() { ResultStatus = SystemEnums.ResultStatus.Fail, Msg = "删除失败" };
        }

        public async Task<BaseResult<RecipeQuaeryResultDto>> GetRecipeListAsyncManager()
        {
            List<RecipeEntity> res = await _recipeService.GetByListAsync(c=>true);
            List<RecipeQuaeryResultDto> dtos = res.Adapt<List<RecipeQuaeryResultDto>>();

            return new BaseResult<RecipeQuaeryResultDto>() { ResultStatus = SystemEnums.ResultStatus.Success, Data = dtos };
            
        }
        public async Task<BaseResult<RecipeQuaeryResultDto>> GetRecipeListByIdAsyncManager(RecipeQuaeryResultByIdDto request)
        {
            RecipeEntity res = await _recipeService.GetByOneAsync(c => c.Id==request.Id);
            RecipeQuaeryResultDto dtos = res.Adapt<RecipeQuaeryResultDto>();

            return new BaseResult<RecipeQuaeryResultDto>() { ResultStatus = SystemEnums.ResultStatus.Success, Data = new List<RecipeQuaeryResultDto>() { dtos } };
        }
    }
}
